<template>
  <layout class-prefix="layout">
    <NumberPad @update:value="onUpdateAmount" @submit="saveRecord"/>
    <Tabs :data-source="recordTypeList"
          :value.sync="record.type"/>
    <div class="notes">
      <FormItem field-name="备注" placeholder="在这里输入备注"
                :value.sync="record.notes"/>

<!--      <FormItem field-name="日期"-->
<!--                type="date"-->
<!--                :value.sync="record.createdAt"/>-->
    </div>
    <Tags @update:value="record.tags = $event"/>
  </layout>
</template>

<script lang="ts">
  import Vue from 'vue';
  import NumberPad from '@/components/Money/NumberPad.vue';
  import FormItem from '@/components/Money/FormItem.vue';
  import Tags from '@/components/Money/Tags.vue';
  import {Component} from 'vue-property-decorator';
  import Tabs from '@/components/Tabs.vue';
  import recordTypeList from '@/constants/recordTypeList';


  @Component({
    components: {Tabs, Tags, FormItem, NumberPad},
  })
  export default class Money extends Vue {
    get recordList() {
      return this.$store.state.recordList;
    }

    recordTypeList = recordTypeList;
    record: RecordItem = {
      tags: [], notes: '', type: '-', amount: 0 , createdAt: new Date().toISOString()
    };

    created() {
      this.$store.commit('fetchRecords');
    }

    onUpdateNotes(value: string) {
      this.record.notes = value;

    }
    onUpdateAmount(value: string) {
      this.record.amount = parseFloat(value);

    }

    saveRecord() {
      if (this.record.type === '+') {
        if (this.record.amount === 0) {
          return window.alert('请输入金额');
        }
      }
      this.$store.commit('createRecord', this.record);

      if (this.$store.state.createRecordError === null) {
        window.alert('已保存');
        this.record.notes = '';
      }

    }

  }
</script>

<style lang="scss" scoped>
  ::v-deep .layout-content {
    display: flex;
    flex-direction: column-reverse;
  }

  .notes {
    padding: 12px 0;
    background: white;
  }
</style>
